<template>
  <div class="page">
    <div class="pay_btn primary-bg mt20" @click="callPayment()">提交订单，去支付</div>
  </div>
</template>

<script>
import WxPlugin from "../utils/wx/wx_snsapi";
import WxPay from "../utils/wx/wx_pay";
import Util from "../utils/util";

export default {
  name: "com",
  components: {},
  props: {},
  data() {
    return {};
  },
  watch: {},
  computed: {},
  methods: {
    callPayment: function() {
      if (!this.verify()) {
        return;
      }
      let _this = this;
      let cookiekey = this.util.getKey("cookiekey");
      let options = {
        url: "/saveorder",
        method: "post",
        headers: { cookiekey: cookiekey },
        data: _this.payForm
      };
      axios(options)
        .then(res => {
          if (res.data.error != 0) {
            alert(res.data.msg);
            return;
          }
          let payway = res.data.data.gateway;
          const { raw } = res.data.data;
          if (payway == "wxpay") {
            _this.wxpay.Pay(raw); //发起微信支付
          } else if (payway == "gxb") {
            _this.gxbpay.Pay(raw); //发起公信宝支付
          }
        })
        .catch(err => {
          alert("prepay err:" + JSON.stringify(err));
        });
    },
    verify: function() {
      let telReg = /^1(3|4|5|6|7|8|9)\d{9}$/;
      if (!this.payForm.phone) {
        alert("手机号不能为空");
        return false;
      } else if (!telReg.test(this.payForm.phone)) {
        alert("手机号格式有误");
        return false;
      }
      if (!this.payForm.name) {
        alert("姓名不能为空");
        return false;
      } else if (this.payForm.name.length > 10) {
        alert("姓名不能超过10个字符");
        return false;
      }
      return true;
    }
  },
  created() {
    this.mywx = new WxPlugin();
    this.wxpay = new WxPay();
    this.util = new Util();

    this.osflag = this.util.isWeixin() ? "wx" : "gxb";
    this.host = this.util.domain;
    this.os = this.util.osCheck();
  },
  mounted() {
    this.$nextTick(function() {
      if (this.util.isWeixin()) {
        this.mywx.pageLoadCheckCode(); // 判断页面是否 微信页面授权，未授权则发起静默授权then请求wxconfig
      } else {
        this.gxb.pageLoadCheckCode(); // 判断页面是否 公信宝页面授权，未授权则发起非静默授权then请求wxconfig
      }
    });
  },
  destroyed() {}
};
</script>
<style lang="scss" scoped>
.page {
}
</style>